import datetime
import decimal
import time

from . import jwtUtils
from .fileSystem import removeFile


# 过滤处理图片路径
def filterImageUrl(request,data,property):
    try:
        ip = jwtUtils.getCurrentIp(request)
        if data[property] and ip:
            data[property] = str(ip)+data[property]
        else:
            # print("没有获取到IP地址")
            data[property] = None
    except Exception as e:
        print(e)
        data[property] = None

# 过滤处理图片路径
def stripImageUrl(request,data,property):
    if data.get(property,None) == None:
        return
    ip = jwtUtils.getCurrentIp(request)
    if data[property] and ip:
        data[property] = data[property].strip(ip)

# 过滤列表的图片路径
def filterDataListImageUrl(request,data,property,innerProperty=None):
    try:
        ip = jwtUtils.getCurrentIp(request)
        for item in data:
            if innerProperty == None:
                if item[property] and ip:
                    item[property] = str(ip)+item[property]
            else:
                if item[innerProperty][property] and ip:
                    item[innerProperty][property] = str(ip)+item[innerProperty][property]
                item[property] = None
    except Exception as e:
        print(e)

# 过滤列表的图片路径
def removeDataListImageUrl(data,property):
    try:
        for item in data:
            try:
                removeFile(item[property])
            except:
                pass
    except Exception as e:
        print(e)

# 获取上传的数据
def filterBodyList(data):
    if type(data) == list:
        pass
    elif type(data) == str:
        data = data.strip("[").strip(']').replace('\"','').replace("\'",'') .split(",")
        # 去除空字符串
        data = [item for item in data if len(item+"") > 0]
    else:
        data = []
    return data

# 获取当前时间
def getCurrentTime():
    return time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())

# 获取当前日期
def getCurrentDate():
    return time.strftime('%Y-%m-%d',time.localtime())

def getDecimalData(data):
    return decimal.Decimal(data)

# 判断是否为空值
def isEmpty(data):
    ignore_list = ['[object Null]','[object Undefined]']
    if data != None:
        if type(data) == int or type(data) == float:
            return False
        elif type(data) == str and len(data) > 0 and not ignore_list.__contains__(data):
            return False
    return True
